
//
//  ZJLoginTextFieldView.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/5/5.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJLoginTextFieldView.h"

@interface ZJLoginTextFieldView ()<UITextFieldDelegate>
/** imageView */
@property (nonatomic, strong) UIImageView *imageView;
@end

@implementation ZJLoginTextFieldView

- (instancetype)initWithFrame:(CGRect)frame imageName:(NSString *)imageName placeholder:(NSString *)placeholder
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.imageView];
        [self addSubview:self.textField];
        
        
        self.imageView.sd_layout
        .centerYEqualToView(self)
        .leftSpaceToView(self, 8.5 * [UIScreen zj_screenWScale])
        .widthIs(16 * [UIScreen zj_screenWScale])
        .heightRatioToView(self, 1);
        
        self.textField.sd_layout
        .centerYEqualToView(self)
        .leftSpaceToView(self.imageView, 13 * [UIScreen zj_screenWScale])
        .rightSpaceToView(self, 9 * [UIScreen zj_screenWScale])
        .heightRatioToView(self, 1);
        
        self.imageView.image = [UIImage imageNamed:imageName];
        self.textField.placeholder = placeholder;
        
        
        self.layer.borderColor = [UIColor zj_colorWithHexString:@"ededed"].CGColor;
        self.layer.borderWidth = 1.0;
        self.sd_cornerRadius = @(4 * [UIScreen zj_screenHScale]);
        
    }
    return self;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    if (toBeString.length >= self.maxNumber && range.length!=1){
        textField.text = [toBeString substringToIndex:self.maxNumber];
        return NO;
        
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];//取消第一响应者
    
    return YES;
}

- (UIImageView *)imageView{
    if (_imageView == nil){
        _imageView = [UIImageView new];
        _imageView.contentMode = UIViewContentModeCenter;
    }
    return _imageView;
}

- (UITextField *)textField{
    if (_textField == nil){
        _textField = [[UITextField alloc] init];
        _textField.font = [UIFont systemFontOfSize:16 * [UIScreen zj_textFontScale]];
        _textField.textColor = [UIColor textBlackColor];
        _textField.delegate = self;
        _textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        _textField.returnKeyType = UIKeyboardTypeTwitter;
        
    }
    return _textField;
}

@end
